File Searcher

© 2005, Gordon Sweet

author contact:

Gordon@gsweet.fsnet.co.uk

Home

Save As w/o Save

Working with Strings

Stylebits Corner

Eddie's Lessons, v8

Multiple Languages

Speech for Disabled

File Searcher

Newsletter help

Index


A while ago I decided to go through all my programs to replace the 100% CPU wastage routine such as

Tim=time$("ms") : while time$("ms") < Tim + 100 : wend

with the preferable such as

calldll #kernel32,"Sleep",100 as ulong,r as void

It was not until I had painstakingly gone through most, that I realised there must be Windows SEARCH function to find the occurrence of the text. However I found the SEARCH under the bottom START button not only awkward to use, but like all such file searches, the results can not be stored for easy subsequent referral. So I concocted the following program, where the results can be stored in any .txt file of your choice including Notepad. This text file is then available for both viewing and printing. You will notice although you can search any files by choosing either a distinct '.ext' or choosing 'all' in a chosen Directory, obviously only readable ASCII files make for suitable searches and retrievals.

The file list is based on adapting DIR.BAS supplied with LB. There is a prompt option allowing you to decide


    nomainwin
    button #1, "Start ", [new], ul 200, 200
    button #1, " Quit ", [quit], ul, 300, 200
    open "Files search" for graphics_fs as #1
    dim item$(1000):dim found$(1000):dim dir$(10,3):dim info$(1, 1)
    #1 "trapclose [quit]; font arial 14 bold; place 200 100"
    #1 "\This will scan all files in any Directory selected by .EXT,"
    #1 "\for any text, and display a list of files containing the text."
    #1 "\It can only work properly on ASCII files such as BAS TXT & LOG,"
    #1 "\and since it only reads the data it cannot corrupt any files."
    playwave "any" : wait
[new]
    close #1
    open "Files Found" for graphics_fs as #1
    #1 "font fixedsys; down; fill white" ' FILL NEEDED TO ALLOW SCROLLING
    redim item$(1000):redim found$(1000):redim dir$(10,3):redim info$(1, 1)
    DefaultDir$ = left$(DefaultDir$,2)+"\" ' START FROM ROOT
    filedialog "Pick by Ext or enter ALL","*.*",File$ 
    if File$ = "" then [quit]
    File$ = upper$(File$)
    type$ = "*." + right$(File$,3) ' SEARCH BY SAME .ext
    if right$(File$,3) = "ALL" then type$ = "*.*"
    sFile$ = noPath$(File$)
    plen = len(File$)-len(sFile$)
    path$ = left$(File$,plen)
    files path$, type$, dir$()
    qtyFiles = val(dir$(0, 0))
    if qtyFiles > 1000 then
        notice "list limited to 1000" 'SET BY DIM (1000)
        qtyFiles = 1000
    end if

    for x = qtyFiles to 1 step -1
        lose = 0
        ts$ = left$(dir$(x, 0) + "            ", 50)
        item$(x) = trim$(ts$)
    next x
    sort item$(), 1,qtyFiles ' ALPHA SORT
    row = 10 : colm = 10
    for z = 1 to qtyFiles ' LIST ALL BY .ext
        #1 "place ";colm;" ";row
        #1 "\";z;" ";left$(item$(z),16)
        colm = colm + 165
        if colm > 900 then colm = 10 : row = row +14
    next z
    #1 "flush; color blue" : row = row + 14
    prompt "Enter text to be found";text$
    cas = 1 : confirm "Ignore upper/lower case?";q$
    found=0 : if q$ = "yes" then cas=0 : text$=upper$(text$)

    for file = 1 to qtyFiles
        #1 "place 350 ";row : #1 "\SEARCHING FILE No. ";file
        tfile$=path$+item$(file)
        open tfile$ for input as #f
        ok=0 : while ok=0
        if EOF(#f) < 0 then ok = 1 : goto [done]
        line input #f, test$ 
        if cas = 0 then test$=upper$(test$) 'IF CASE IGNORED
        if instr(test$,text$)>0 then
            found = found + 1
            found$(found) = item$(file) : ok = 1
        end if
[done] wend : close #f
    next file

    #1 "place 350 ";row
    #1 "\'";text$;"' FOUND IN THE FOLLOWING ";found;"      "
    row = row + 14 : colm = 10
    for z = 1 to found
        #1 "place ";colm;" ";row
        #1 "\";found$(z)
        colm = colm + 165
        if colm > 900 then colm = 10 : row = row +14
    next z : #1 "flush"

    filedialog "Create txt file to store list", "*.txt", F$
    if F$ = "" then [quit]
    open F$ for output as #s
    for n = 1 TO found
        #s n;" ";found$(n)
    NEXT : close #s :  notice "FILE " + F$ + " SAVED."

[quit]
    confirm "Another list?"; list$
    if list$ = "yes" then [new]
    close #1 : end

    function noPath$(t$)
    while instr(t$, "\")
        t$ = mid$(t$, 2)
    wend
    noPath$ = t$
    end function


The file filesearcher.bas is included in the zipped archive of this newsletter.